import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx';
import dotEnv from 'dotenv'
import path from 'path'

dotEnv.config()
// https://vitejs.dev/config/
export default defineConfig(({command, mode}) => {
  // if(command === 'serve') {
  //   return {
  //     // server 独有配置
  //     plugins: [vue()]
  //   }
  // } else {
  //   return {
  //     // build独有配置
  //     plugins: [vue()]
  //   }
  // }

  if(mode === 'development') {
    console.log('dev')
    console.log('env varibale', process.env.DB_PASS)
    return {
      plugins: [vue(), vueJsx()],
      resolve: {
        alias: {
          'utils': path.resolve(__dirname, 'src/utils'),
          'style': path.resolve(__dirname, 'style')
        },
        extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
        mainFields: ['module','jsnext:main', 'jsnext'],
      },
      server: {
        proxy: {
          // 字符串简写写法
          '/foo': 'http://112.126.100.206',
          '/api': {
            target: 'http://112.126.100.206',
            changeOrigin: true, // 跨域
            rewrite: (path) => path.replace(/^\/api/, '')
          }
        }
      },
      optimizeDeps: {
        // 如果取消注释这行，可以模拟取消预购建
        // exclude: ['lodash-es']
      }
    }
  } else if(mode === 'production') {
    console.log('pro')
    return {
      plugins: [vue(), vueJsx()],
      resolve: {
        alias: {
          'utils': path.resolve(__dirname, 'src/utils'),
          'style': path.resolve(__dirname, 'style')
        },
        extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'],
        mainFields: ['module','jsnext:main', 'jsnext'],
      },
      build: {
        target: 'esnext',
        rollupOptions: {
          // 生产环境不到包的依赖，减少提及
          external: ['react']
        }
      },
      esbuild: {
        jsxFactory: 'h',
        jsxFragment: 'Fragment'
      },
    }
  } else {
    console.log('enviroment is',mode)
    return {
      mode: 'sit',
      plugins: [vue()]
    }
  }
})
